﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar.Metro;
using DevComponents.DotNetBar.Metro.ColorTables;
using PlanningGIS.Framework.Config;
using PlanningGIS.ManageUtility;
using PlanningGIS.ManageUtility.DataAccess;
using PlanningGIS.ManageUtility.Modules;
using PlanningGIS.Util.Data;
using PlanningGIS.Util.Log;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.Manager
{
    /// <summary>
    /// 维护系统主窗体
    /// </summary>
    public partial class FrmMain : MetroAppForm
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmMain()
        {
            InitializeComponent();
            InitControl();
        }

        #region 窗体事件
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            UpdateControlsSizeAndLocation();
            base.OnLoad(e);
            InitData();
        }

        /// <summary>
        /// 大小改变
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {
            UpdateControlsSizeAndLocation();
            base.OnResize(e);
        }

        /// <summary>
        /// 更新控件的大小和位置
        /// </summary>
        private void UpdateControlsSizeAndLocation()
        {
            if (_StartControl != null)
            {
                if (!_StartControl.IsOpen)
                    _StartControl.OpenBounds = GetStartControlBounds();
                else
                    _StartControl.Bounds = GetStartControlBounds();
                if (!IsModalPanelDisplayed)
                    _StartControl.BringToFront();
            }
        }

        /// <summary>
        /// 获取开始控件的矩形范围
        /// </summary>
        /// <returns></returns>
        private Rectangle GetStartControlBounds()
        {
            int captionHeight = metroShell.MetroTabStrip.GetCaptionHeight() + 2;
            Thickness borderThickness = this.GetBorderThickness();
            return new Rectangle((int)borderThickness.Left, captionHeight, this.Width - (int)borderThickness.Horizontal, this.Height - captionHeight - 1);
        }

        #endregion

        #region 属性和变量
        /// <summary>
        /// 启动界面
        /// </summary>
        private StartControl _StartControl = null;

        /// <summary>
        /// 所有表单的命令
        /// </summary>
        private BillCommands _Commands = null;
        #endregion

        #region 初始化
        /// <summary>
        /// 初始化系统数据
        /// </summary>
        private void InitData()
        {
            try
            {
                ManageHelper.Hash_Tables = ManageHelper.DBContext.GetAllEntry<Hash_Table>();
                ManageHelper.PrivilegeCfg = ConfigHelper.GetObject<PrivilegeConfig>();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorText(ex);
            }
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            _Commands = new BillCommands();
            _Commands.ChangeTheme = new Command(components, new EventHandler(ChangeThemeExecuted));
            
            _Commands.DepartmentCommands.New = new Command(components);
            _Commands.DepartmentCommands.New.Executed += NewDepartment_Executed;
            _Commands.DepartmentCommands.Save = new Command(components);
            _Commands.DepartmentCommands.Save.Executed += SaveDepartment_Executed;
            _Commands.DepartmentCommands.Cancel = new Command(components);
            _Commands.DepartmentCommands.Cancel.Executed += CancelDepartment_Executed;

            _Commands.FunctionCommands.New = new Command(components);
            _Commands.FunctionCommands.New.Executed += NewFunction_Executed;
            _Commands.FunctionCommands.Save = new Command(components);
            _Commands.FunctionCommands.Save.Executed += SaveFunction_Executed;
            _Commands.FunctionCommands.Cancel = new Command(components);
            _Commands.FunctionCommands.Cancel.Executed += CancelFunction_Executed;

            _Commands.TocCommands.New = new Command(components);
            _Commands.TocCommands.New.Executed += NewToc_Executed;
            _Commands.TocCommands.Save = new Command(components);
            _Commands.TocCommands.Save.Executed += SaveToc_Executed;
            _Commands.TocCommands.Cancel = new Command(components);
            _Commands.TocCommands.Cancel.Executed += CancelToc_Executed;

            _Commands.DataCommands.New = new Command(components);
            _Commands.DataCommands.New.Executed += NewData_Executed;
            _Commands.DataCommands.Save = new Command(components);
            _Commands.DataCommands.Save.Executed += SaveData_Executed;
            _Commands.DataCommands.Cancel = new Command(components);
            _Commands.DataCommands.Cancel.Executed += CancelData_Executed;
            
            _Commands.RoleCommands.New = new Command(components);
            _Commands.RoleCommands.New.Executed += NewRole_Executed;
            _Commands.RoleCommands.Save = new Command(components);
            _Commands.RoleCommands.Save.Executed += SaveRole_Executed;
            _Commands.RoleCommands.Cancel = new Command(components);
            _Commands.RoleCommands.Cancel.Executed += CancelRole_Executed;

            _Commands.UserCommands.New = new Command(components);
            _Commands.UserCommands.New.Executed += NewUser_Executed;
            _Commands.UserCommands.Save = new Command(components);
            _Commands.UserCommands.Save.Executed += SaveUser_Executed;
            _Commands.UserCommands.Cancel = new Command(components);
            _Commands.UserCommands.Cancel.Executed += CancelUser_Executed;

            _Commands.AdUserCommands.New = new Command(components);
            _Commands.AdUserCommands.New.Executed += AdUser_Executed;


            _Commands.LogCommands.New = new Command(components);
            _Commands.LogCommands.New.Executed += NewLog_Executed;
            _Commands.LogCommands.Save = new Command(components);
            _Commands.LogCommands.Save.Executed += SaveLog_Executed;
            _Commands.LogCommands.Cancel = new Command(components);
            _Commands.LogCommands.Cancel.Executed += CancelLog_Executed;

            _Commands.SecuritySetCommands.New = new Command(components);
            _Commands.SecuritySetCommands.New.Executed += NewSecuritySet_Executed;
            _Commands.SecuritySetCommands.Save = new Command(components);
            _Commands.SecuritySetCommands.Save.Executed += SaveSecuritySet_Executed;
            _Commands.SecuritySetCommands.Cancel = new Command(components);
            _Commands.SecuritySetCommands.Cancel.Executed += CancelSecuritySet_Executed;

            _Commands.MXDCommands.New = new Command(components);
            _Commands.MXDCommands.New.Executed += NewMXD_Executed;
            _Commands.MXDCommands.Save = new Command(components);
            _Commands.MXDCommands.Save.Executed += SaveMXD_Executed;
            _Commands.MXDCommands.Cancel = new Command(components);
            _Commands.MXDCommands.Cancel.Executed += CancelMXD_Executed;


            this.SuspendLayout();
            _StartControl = new StartControl();
            _StartControl.Commands = _Commands;
            this.Controls.Add(_StartControl);
            _StartControl.BringToFront();
            _StartControl.SlideSide = DevComponents.DotNetBar.Controls.eSlideSide.Right;
            //_StartControl.Click += new EventHandler(StartControl_Click);
            this.ResumeLayout(false);

            MetroColorGeneratorParameters[] metroThemes = MetroColorGeneratorParameters.GetAllPredefinedThemes();
            foreach (MetroColorGeneratorParameters mt in metroThemes)
            {
                ButtonItem theme = new ButtonItem(mt.ThemeName, mt.ThemeName);
                theme.Command = _Commands.ChangeTheme;
                theme.CommandParameter = mt;
                mnuColors.SubItems.Add(theme);
            }
        }

        /// <summary>
        /// 改变主题颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangeThemeExecuted(object sender, EventArgs e)
        {
            ICommandSource source = (ICommandSource)sender;
            MetroColorGeneratorParameters theme = (MetroColorGeneratorParameters)source.CommandParameter;
            StyleManager.MetroColorGeneratorParameters = theme;
        }

        #region 部门管理
        /// <summary>
        /// 部门管理控件
        /// </summary>
        private CtlDepartment _Department = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NewDepartment_Executed(object sender, EventArgs e)
        {
            if (!ManageHelper.CurrentUser.IsAdministrator() && !ManageHelper.CurrentUser.IsSecurityAdmin() 
                && ManageHelper.SecuritySet.SplitUser)
            {
                MsgHelper.ShowErrorMsgbox("您不是安全保密管理员，不能使用该功能!");
                return;
            }           
            _Commands.DepartmentCommands.New.Enabled = false;
            _Department = new CtlDepartment();
            _Department.Commands = _Commands;
            _Department.LoadControl();
            this.ShowModalPanel(_Department, eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SaveDepartment_Executed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CancelDepartment_Executed(object sender, EventArgs e)
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            _Commands.DepartmentCommands.New.Enabled = true; // Enable new client command
            this.CloseModalPanel(_Department, DevComponents.DotNetBar.Controls.eSlideSide.Left);
            _Department.Commands = null;
            _Department.Dispose();
            _Department = null;
        }

        #endregion

        #region 功能权限
        /// <summary>
        /// 部门管理控件
        /// </summary>
        private CtlFunction _Function = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NewFunction_Executed(object sender, EventArgs e)
        {
            if (!ManageHelper.CurrentUser.IsAdministrator() && !ManageHelper.CurrentUser.IsSystemAdmin()
                && ManageHelper.SecuritySet.SplitUser)
            {
                MsgHelper.ShowErrorMsgbox("您不是系统管理员，不能使用该功能!");
                return;
            }   
            this.Cursor = Cursors.WaitCursor;
            _Commands.FunctionCommands.New.Enabled = false;
            _Function = new CtlFunction();
            _Function.Commands = _Commands;
            _Function.LoadControl();
            Application.DoEvents();
            this.ShowModalPanel(_Function, eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SaveFunction_Executed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CancelFunction_Executed(object sender, EventArgs e)
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            _Commands.FunctionCommands.New.Enabled = true; // Enable new client command
            this.CloseModalPanel(_Function, eSlideSide.Left);
            _Function.Commands = null;
            _Function.Dispose();
            _Function = null;
        }

        #endregion

        #region Toc权限管理
        /// <summary>
        /// 部门管理控件
        /// </summary>
        private CtlToc _Toc = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NewToc_Executed(object sender, EventArgs e)
        {
            if (!ManageHelper.CurrentUser.IsAdministrator() && !ManageHelper.CurrentUser.IsSystemAdmin()
                && ManageHelper.SecuritySet.SplitUser)
            {
                MsgHelper.ShowErrorMsgbox("您不是系统管理员，不能使用该功能!");
                return;
            }   
            _Commands.TocCommands.New.Enabled = false;
            _Toc = new CtlToc();
            _Toc.Commands = _Commands;
            _Toc.LoadControl();
            this.ShowModalPanel(_Toc, eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SaveToc_Executed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CancelToc_Executed(object sender, EventArgs e)
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            _Commands.TocCommands.New.Enabled = true; // Enable new client command
            this.CloseModalPanel(_Toc, eSlideSide.Left);
            _Toc.Commands = null;
            _Toc.Dispose();
            _Toc = null;
        }

        #endregion
        
        #region 数据权限
        /// <summary>
        /// 部门管理控件
        /// </summary>
        private CtlData _Data = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NewData_Executed(object sender, EventArgs e)
        {
            if (!ManageHelper.CurrentUser.IsAdministrator() && !ManageHelper.CurrentUser.IsSystemAdmin()
                && ManageHelper.SecuritySet.SplitUser)
            {
                MsgHelper.ShowErrorMsgbox("您不是系统管理员，不能使用该功能!");
                return;
            }   
            _Commands.DataCommands.New.Enabled = false;
            _Data = new  CtlData();
            _Data.Commands = _Commands;
            _Data.LoadControl();
            this.ShowModalPanel(_Data, eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SaveData_Executed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CancelData_Executed(object sender, EventArgs e)
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            _Commands.DataCommands.New.Enabled = true; // Enable new client command
            this.CloseModalPanel(_Data, eSlideSide.Left);
            _Data.Commands = null;
            _Data.Dispose();
            _Data = null;
        }

        #endregion

        #region 角色管理
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NewRole_Executed(object sender, EventArgs e)
        {
            if (!ManageHelper.CurrentUser.IsAdministrator() && !ManageHelper.CurrentUser.IsSecurityAdmin()
                && ManageHelper.SecuritySet.SplitUser)
            {
                MsgHelper.ShowErrorMsgbox("您不是安全保密管理员，不能使用该功能!");
                return;
            }   
            if (ctlRoles1.Commands == null)
            {
                ctlRoles1.Commands = _Commands;                
            }
            ctlRoles1.BindData();
            ctlRoles1.RefreshControl();
            metroShell.SelectedTab = mnutbitmRole;
            _StartControl.IsOpen = !_StartControl.IsOpen;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SaveRole_Executed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CancelRole_Executed(object sender, EventArgs e)
        {
            _StartControl.IsOpen = !_StartControl.IsOpen;
            //if (!_StartControl.Visible)
            //    _StartControl.SlideOutButtonVisible = true;
            //_Commands.DataCommands.New.Enabled = true; // Enable new client command
            //this.CloseModalPanel(_Roles, eSlideSide.Left);
            //_Roles.Commands = null;
            //_Roles.Dispose();
            //_Roles = null;
        }

        #endregion

        #region 用户管理
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NewUser_Executed(object sender, EventArgs e)
        {
            if (ManageHelper.CurrentUser.IsAdministrator() || ManageHelper.CurrentUser.IsSecurityAdmin() || ManageHelper.CurrentUser.IsSystemAdmin()
                || !ManageHelper.SecuritySet.SplitUser)
            {
                FrmUser frm = new FrmUser();
                frm.ShowDialog(this);
            }
            else
            {
                MsgHelper.ShowErrorMsgbox("您不是安全保密管理员，不能使用该功能!");
                return;
            }


        }
        void AdUser_Executed(object sender, EventArgs e)
        {
            if(MsgHelper.ShowYesOrNoQueryMsgBox("确实要同步AD域用户信息吗？")!= DialogResult.Yes)
            {
                return;
            }

            string adServer= System.Configuration.ConfigurationManager.AppSettings.Get("AdServer");
            string loginname = System.Configuration.ConfigurationManager.AppSettings.Get("AdLoginName");
            string psd = System.Configuration.ConfigurationManager.AppSettings.Get("AdPassword");
            DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + adServer,loginname, psd, AuthenticationTypes.Secure);


            System.Collections.IEnumerator era = adRoot.Children.GetEnumerator();
            ManageHelper.DBContext.Delete("department");
           int c=  ManageHelper.DBContext.ExecuteNonQuery("delete from userinfo where  login_name<>'admin'");
            while (era.MoveNext())
            {
                DirectoryEntry ou = era.Current as DirectoryEntry;
                if (ou.SchemaClassName == "organizationalUnit" || ou.SchemaClassName == "container")
                {

                    Department org = new Department();
                    org.ID = ou.Guid.ToString();
                    org.Name = ou.Properties["name"][0].ToString();
                    ManageHelper.DBContext.Insert(org);
                 
                    GetSub(ou);
                }
            }
        



//        while (era.MoveNext())
//        {
//            DirectoryEntry ou = era.Current as DirectoryEntry;
//            if (ou.SchemaClassName == "organizationalUnit")
//            {


//                DirectorySearcher mySearcher = new DirectorySearcher(ou);//想搜索出所有，此处可省参数

//        mySearcher.Filter = ("(objectClass=user)"); //user表示用户，group表示组

//                foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
//                {


//                    DirectoryEntry user = resEnt.GetDirectoryEntry();
//                    if (user.SchemaClassName.ToLower() != "user") continue;


//                     string loginName = user.Properties["sAMAccountName"][0].ToString();
//        string displayName = user.Properties["displayName"][0].ToString();
//                    if (UserAccess.IsSameLoginName(loginName)==false )
//                    {
//                        UserInfo u = new UserInfo()
//                        {
//                            Login_Name = loginName,
//                            Name = displayName,
//                            Status = "unlock",
//                            Admin_Type = "FFF",
//                            IsActive = "1",
//                            EffectiveTime = DateTime.Now,
//                            ID = System.Guid.NewGuid().ToString()
//                        };
//        ManageHelper.DBContext.Insert<UserInfo>(u);

//                    }



//}
//        }
//    }
    MsgHelper.ShowInfomationMsgbox("同步AD域用户成功！");
}

        public void GetSub(DirectoryEntry entry)
        {
            foreach (DirectoryEntry subEntry in entry.Children)
            {
                if (subEntry.SchemaClassName == "organizationalUnit" || subEntry.SchemaClassName == "container")
                {
                    Department org = new  Department();
                    org.ID = subEntry.Guid.ToString();
                    org.Name = subEntry.Properties["name"][0].ToString();
                    org.Parent_ID = entry.Guid.ToString();

                    ManageHelper.DBContext.Insert(org);

                    
                }
                else if (subEntry.SchemaClassName == "user")
                {
                    UserInfo user = new UserInfo();
                    user.ID = subEntry.Guid.ToString();
                    user.Login_Name = subEntry.Properties["sAMAccountName"][0].ToString();
                    user.Name = subEntry.Properties.Contains("displayname") ? subEntry.Properties["displayname"][0].ToString() : "";
                    user.MobilePhone = subEntry.Properties.Contains("telephoneNumber") ? subEntry.Properties["telephoneNumber"][0].ToString() : "";
                    user.DepartMent_ID = entry.Guid.ToString();
                    user.Status = "unlock";
                    user.Admin_Type = "FFF";
                    user.IsActive = "1";
                    user.EffectiveTime = DateTime.Now;
                    if (UserAccess.IsSameLoginName(user.Login_Name) == false)
                        ManageHelper.DBContext.Insert(user);
                  //  else
                        //ManageHelper.DBContext.Update()
                }
            }
            //organizationalUnit
        }






        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SaveUser_Executed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CancelUser_Executed(object sender, EventArgs e)
        {
            //if (!_StartControl.Visible)
            //    _StartControl.SlideOutButtonVisible = true;
            //_Commands.UserCommands.New.Enabled = true; // Enable new client command
            //this.CloseModalPanel(_Users, eSlideSide.Left);
            //_Users.Commands = null;
            //_Users.Dispose();
            //_Users = null;
            _StartControl.IsOpen = !_StartControl.IsOpen;
        }

        #endregion

        #region 日志管理
        /// <summary>
        /// 部门管理控件
        /// </summary>
        private CtlLog _Log = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NewLog_Executed(object sender, EventArgs e)
        {
            if (!ManageHelper.CurrentUser.IsAdministrator() && !ManageHelper.CurrentUser.IsAuditAdmin()
                && ManageHelper.SecuritySet.SplitUser)
            {
                MsgHelper.ShowErrorMsgbox("您不是安全审计员，不能使用该功能!");
                return;
            }   
            _Commands.LogCommands.New.Enabled = false;
            _Log = new CtlLog();
            _Log.Commands = _Commands;
            this.ShowModalPanel(_Log, eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SaveLog_Executed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CancelLog_Executed(object sender, EventArgs e)
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            _Commands.LogCommands.New.Enabled = true; // Enable new client command
            this.CloseModalPanel(_Log, eSlideSide.Left);
            _Log.Commands = null;
            _Log.Dispose();
            _Log = null;
        }

        #endregion

        #region 安全设置
        /// <summary>
        /// 安全设置控件
        /// </summary>
        private CtlSecuritySet _SecuritySet = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NewSecuritySet_Executed(object sender, EventArgs e)
        {
            if (!ManageHelper.CurrentUser.IsAdministrator() && !ManageHelper.CurrentUser.IsSecurityAdmin()
                && ManageHelper.SecuritySet.SplitUser)
            {
                MsgHelper.ShowErrorMsgbox("您不是安全保密管理员，不能使用该功能!");
                return;
            }   
            _Commands.SecuritySetCommands.New.Enabled = false;
            _SecuritySet = new CtlSecuritySet();
            _SecuritySet.Commands = _Commands;
            _SecuritySet.LoadControl();
            this.ShowModalPanel(_SecuritySet, eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SaveSecuritySet_Executed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CancelSecuritySet_Executed(object sender, EventArgs e)
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            _Commands.SecuritySetCommands.New.Enabled = true; // Enable new client command
            this.CloseModalPanel(_SecuritySet, eSlideSide.Left);
            _SecuritySet.Commands = null;
            _SecuritySet.Dispose();
            _SecuritySet = null;
        }

        #endregion

        #region 配图模板
        /// <summary>
        /// 
        /// </summary>
        private CtlMXD _MXD = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NewMXD_Executed(object sender, EventArgs e)
        {
            if (!ManageHelper.CurrentUser.IsAdministrator() && !ManageHelper.CurrentUser.IsSystemAdmin()
                && ManageHelper.SecuritySet.SplitUser)
            {
                MsgHelper.ShowErrorMsgbox("您不是系统管理员，不能使用该功能!");
                return;
            }   
            this.Cursor = Cursors.WaitCursor;
            _Commands.MXDCommands.New.Enabled = false;
            _MXD = new CtlMXD();
            _MXD.Commands = _Commands;
            _MXD.LoadControl();
            Application.DoEvents();
            this.ShowModalPanel(_MXD, eSlideSide.Left);
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = false;
            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SaveMXD_Executed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CancelMXD_Executed(object sender, EventArgs e)
        {
            if (!_StartControl.Visible)
                _StartControl.SlideOutButtonVisible = true;
            _Commands.MXDCommands.New.Enabled = true; // Enable new client command
            this.CloseModalPanel(_MXD, eSlideSide.Left);
            _MXD.Commands = null;
            _MXD.Dispose();
            _MXD = null;
        }

        #endregion

        #endregion

        #region 控件操作
        /// <summary>
        /// 主界面Tab页选择改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void metroShell_SelectedTabChanged(object sender, EventArgs e)
        {
            UpdateControlsSizeAndLocation();
        }
        /// <summary>
        /// 点击启动界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void StartControl_Click(object sender, EventArgs e)
        {
            _StartControl.IsOpen = false;
        }
        #endregion

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MsgHelper.ShowYesOrNoQueryMsgBox("是否确定要退出该系统？") != System.Windows.Forms.DialogResult.Yes)
            {
                e.Cancel = true;
                return;
            }

        }
        
    }

}
